<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 19-4-20
 * Time: 下午9:09
 */

namespace app\admin\model;


use think\Model;
use tool\Csv;

class Rating extends Model
{


    /**
     * @$type_rating_arr array
     * 评分标准
     */
    private $type_rating_arr = [
        'click'=> 5.0,
        'comment'=>6.0,
        'collect'=>7.0
    ];



    /**
     * 插入评分的数据
     * @param int $uid 用户id
     * @param int $aid 文章id
     * @param string $tra 评分
     * @return mixed int|false
     * */
    public function add($uid,$aid,$tra){
        //参数不可以为空
        if (!empty($uid)&&!empty($aid)&&!empty($tra)){
            $data=[
                'll_uid'=>$uid,
                'll_aid'=>$aid,
                'll_rating'=>$this->type_rating_arr[$tra],
                'll_time'=>time()
            ];
            return $this->isUpdate(false)->save($data) ;
        }else{
            return false;
        }
    }

    /**
     * 导出csv
     *
     *
     * **/
    public function csv(){
        $csv = new Csv();
        $ratingmodel = new \app\admin\model\Rating();
        $list =  $ratingmodel->field('ll_uid,ll_aid,ll_rating,ll_time')->select();
        $list = collection($list)->toArray();
        $csv_title = array('userId','AID','rating','timestamp');
        $csv->put_csv($list,$csv_title);
    }


}